package com.dd.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.DdShopgoods;
import com.dd.util.PageList;

/**
 * A data access object (DAO) providing persistence and search support for
 * DdShopgoods entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.dd.entity.DdShopgoods
 * @author MyEclipse Persistence Tools
 */

public class DdShopgoodsDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DdShopgoodsDAO.class);
	// property constants
	public static final String SHOPID = "shopid";
	public static final String TYPE = "types";
	public static final String NAME = "name";
	public static final String DESCRIPTION = "description";
	public static final String PIC1 = "pic1";
	public static final String PIC2 = "pic2";
	public static final String PIC3 = "pic3";
	public static final String PRICE = "price";

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(DdShopgoods transientInstance) {
		log.debug("saving DdShopgoods instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public void delete(DdShopgoods persistentInstance) {
		log.debug("deleting DdShopgoods instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DdShopgoods findById(java.lang.Integer id) {
		log.debug("getting DdShopgoods instance with id: " + id);
		try {
			DdShopgoods instance = (DdShopgoods) getHibernateTemplate().get(
					"com.dd.entity.DdShopgoods", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding DdShopgoods instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from DdShopgoods as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByShopidAndDay(int shopid,int days) {
		log.debug("findByShopid");
		try {
			return getHibernateTemplate()
					.find("from DdShopgoods where shopid =" + shopid+ " and style="+days);
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}

	}


	public List findByShopid(int shopid) {
		log.debug("findByShopid");
		try {
			return getHibernateTemplate()
					.find("from DdShopgoods where shopid =" + shopid+ " order by id");
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}

	}

	public PageList findByShopid(int shopid, int pageNo, int pageSize) {
		log.debug("finding all DdOrder instances");
		try {
			PageList pl = new PageList();
			String queryString = "from DdShopgoods a, DdShop b where 1=1 and a.shopid=b.id and shopid=" + shopid;
			String maxsizeStr = (" select count(*) " + queryString);
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			q = this.getSession().createQuery(
					queryString + " order by a.id desc");
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}

	}

	public List findByShopAndName(int shopid, String goodname) {
		log.debug("findByShopid");
		try {
			return getHibernateTemplate().find(
					"from DdShopgoods where shopid =" + shopid + " and name='"
							+ goodname + "'" + " order by id");
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}

	}

	public List findAll() {
		log.debug("finding all DdShopgoods instances");
		try {
			String queryString = "from DdShopgoods";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

}